package com.shujia.day13;

import java.util.Scanner;
import java.util.TreeSet;

public class HStudentScoreOrder {
    public static void main(String[] args) {
        //  键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
        /*
            1、创建学生对象
            2、存在四个属性
            3、当前学生对象可以进行排序，并且要自定义其排序的逻辑
         */

        TreeSet<HStudent> hStudents = new TreeSet<>();
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入学生姓名：");
            String name = scanner.next();
            System.out.println("请输入语文成绩：");
            int chinese = scanner.nextInt();
            System.out.println("请输入数学成绩：");
            int math = scanner.nextInt();
            System.out.println("请输入英语成绩：");
            int english = scanner.nextInt();
            hStudents.add(new HStudent(name,chinese,math,english));
        }

        System.out.println(hStudents);

    }
}

//
class HStudent implements Comparable<HStudent>{
    String name;
    int chinese;

    int math;

    int english;

    public HStudent() {
    }

    public HStudent(String name, int chinese, int math, int english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }


    @Override
    public int compareTo(HStudent that) {
        int thisScore =  this.chinese + this.english+this.math ;
        int thatScore =  that.chinese + that.english+that.math ;

        // this - that 是升序
        return  thatScore - thisScore == 0 ? this.name.compareTo(that.name): thatScore - thisScore ;
    }

    @Override
    public String toString() {
        return "HStudent{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}';
    }
}
